package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.dto.SetmealDTO;
import com.sky.dto.SetmealPageQueryDTO;
import com.sky.entity.Dish;
import com.sky.entity.Setmeal;
import com.sky.entity.SetmealDish;
import com.sky.exception.DeletionNotAllowedException;
import com.sky.exception.SetmealEnableFailedException;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.mapper.SetmealDishMapper;
import com.sky.result.PageResult;
import com.sky.service.SetmealService;
import com.sky.vo.SetmealVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.util.Arrays;
import java.util.List;

import static com.sky.constant.MessageConstant.*;
import static com.sky.constant.StatusConstant.*;


@Service
public class SetmealServiceImpl implements SetmealService {


    @Autowired
    private SetmealMapper setmealMapper;
    @Autowired
    private SetmealDishMapper setmealdishMapper;
    @Autowired
    private DishMapper dishMapper;


    /**
     * 新增套餐
     */
    @Transactional
    public void add(SetmealDTO setmealDTO) {
        //创建套餐对象
        Setmeal setmeal = new Setmeal();
        //给套餐对象赋值
        BeanUtils.copyProperties(setmealDTO,setmeal);
        setmeal.setStatus(DISABLE);
        //调用mapper插入
        setmealMapper.add(setmeal);
        //创建套餐菜品表对象 List
        List<SetmealDish> setmealDishes = setmealDTO.getSetmealDishes();
        //判断前端穿来的数据是否为空
        if(!CollectionUtils.isEmpty(setmealDishes)){
            //如果不为空，给套餐菜品表对象赋值（赋值套餐id）
            setmealDishes.forEach(setmealDish -> {
                setmealDish.setSetmealId(setmeal.getId());
            });
            //调用mapper插入
            setmealdishMapper.addBatch(setmealDishes);
        }
    }


    /**
     * 分页查询
     */
    public PageResult page(SetmealPageQueryDTO dto) {
        //开启分页
        PageHelper.startPage(dto.getPage(),dto.getPageSize());
        //调用mapper插入
        Page<SetmealVO> setmealVOS = setmealMapper.page(dto);
        //封装PageResult返回
        return new PageResult(setmealVOS.getTotal(),setmealVOS.getResult());
    }


    /**
     * 批量删除套餐
     */
    @Transactional
    public void deleteBatch(List<Long> ids) {
        //根据id查询套餐状态，若果是在售的不能删 抛异常
        Integer idNum = setmealMapper.getStatusById(ids);
        if(idNum > 0){
            throw new DeletionNotAllowedException(SETMEAL_ON_SALE);
        }
        //不是在售的调用mapper删除
        setmealMapper.deleteBatch(ids);
        //调用mapper删除套餐菜品表中的信息
        setmealdishMapper.deleteBatchBySetmealId(ids);
    }



    /**
     * 根据id查询套餐(查询回显)
     */
    public SetmealVO getById(Long id) {
        //调用mapper查询套餐
        Setmeal setmeal = setmealMapper.getById(id);
        //调用mapper查询套餐菜品连接表数据库中的数据
        List<SetmealDish> setmealDishes = setmealdishMapper.getBySetmealId(id);
        //封装SetmealVO返回
        SetmealVO setmealVO = new SetmealVO();
        BeanUtils.copyProperties(setmeal,setmealVO);
        setmealVO.setSetmealDishes(setmealDishes);
        return setmealVO;
    }


    /**
     * 修改套餐
     */
    public void update(SetmealDTO setmealDTO) {
        //创建套餐对象
        Setmeal setmeal = new Setmeal();
        //给套餐对象赋值
        BeanUtils.copyProperties(setmealDTO,setmeal);
        //调用mapper修改
        setmealMapper.update(setmeal);
        //删除旧的套餐菜品表数据
        setmealdishMapper.deleteBatchBySetmealId(Arrays.asList(setmeal.getId()));
        //创建套餐菜品表对象 List
        List<SetmealDish> setmealDishes = setmealDTO.getSetmealDishes();
        //判断前端穿来的数据是否为空
        if(!CollectionUtils.isEmpty(setmealDishes)){
            //如果不为空，给套餐菜品表对象赋值（赋值套餐id）
            setmealDishes.forEach(setmealDish -> {
                setmealDish.setSetmealId(setmeal.getId());
            });
            //调用mapper插入
            setmealdishMapper.addBatch(setmealDishes);
        }
    }


    /**
     * 套餐起售、停售
     */
    public void updateStatus(Integer status, Long id) {
        //判断是要起售还是停售
        if(status == ENABLE){
            //如果是起售，判断套餐中所包含的菜品是否有停售的，如果有抛异常
            List<Dish> dishes = dishMapper.getBySetmealId(id);
            //判断菜品列表是否为空
            if(dishes != null && dishes.size()>0){
                dishes.forEach(dish -> {
                    //判断如果有停售的菜品就不能修改状态 抛异常
                    if(dish.getStatus().equals(DISABLE)){
                        throw new SetmealEnableFailedException(SETMEAL_ENABLE_FAILED);
                    }
                });
            }
        }
        //如果是停售，创建套餐对象给套餐对象赋值调用mapper进行修改
        Setmeal setmeal = Setmeal.builder()
                .id(id)
                .status(status)
                .build();
        setmealMapper.update(setmeal);
    }
}
